Component({
  mixins: [],
  data: {
    listData1: [
    ],
    listData2: [],
    listData3: [],
    activeKey: 1,
    showModal1: true,
    showModal2: false,
    showModal3: false,
    page: 1,
    pagesize: 10,
    shop_total: '',
    total2: 0,
    total3: 0,
    modals1: false,
    modals2: false,
    modals3: false,
    name: '',//#endregion
    selectTime: ['2020-03-08', '2020-03-18'],
    item: 1,
    denominations: 0,
    condition: '',
    user_item: '',
    kindName: '',
    kindPic: '',
    kindLink: '',
    kindItem: '',
    kindUser_item: ''
  },
  props: {},
  didMount() {
    this.getShopCouponList(1);
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    /** 上传图片 */
    uploadFile() {
      my.chooseImage({
        chooseImage: 1,
        success: res => {
          const path = res.apFilePaths[0];
          console.log(path);
          my.uploadFile({
            url: 'http://httpbin.org/post',
            fileType: 'image',
            fileName: 'file',
            filePath: path,
            success: res => {
              my.alert({ title: '上传成功', content: "上传成功" });
            },
            fail: function (res) {
              my.alert({ title: '上传失败', content: "上传失败" });
            },
          });
        },
      });
    },
    //关闭模态框
    closeModel(e) {
      var name = e.currentTarget.dataset.name;
      this.setData({ [name]: false });
    },
    openModel(e) {
      var name = e.currentTarget.dataset.name;
      this.setData({ [name]: true });
    },
    /**
     * 店铺优惠券
     */
    handleSubmit1() {
      if (!this.data.name) {
        my.alert({ content: '优惠券名称不能为空!' });
        return false;
      }
      if (!this.data.item) {
        my.alert({ content: '请设置一个发放数量!' });
        return false;
      }
      if (!this.data.condition) {
        my.alert({ content: '请设置使用门槛!' });
        return false;
      }
      if (!this.data.user_item) {
        my.alert({ content: '请设置每人限量!' });
        return false;
      }
      //my.alert({ content: JSON.stringify(this.data) });
      var apiurl = 'App.ShopRedsInfos.SaveUserCoupon';
      var params = {
        "name": this.data.name,
        "item": this.data.item,
        'start_time': this.data.selectTime[0],
        'end_time': this.data.selectTime[1],
        "denominations": this.data.denominations,
        "user_item": this.data.user_item,
        "condition": this.data.condition
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
         self.setData({
          modals1:false
        })
        self.getShopCouponList(1)
        my.alert({ title: '店铺优惠券创建成功!' });
      });
    },
    /**
     * 实物礼品
     */
    handleSubmit3() {
      if (!this.data.kindName) {
        my.alert({ content: '实物名称不能为空!' });
        return false;
      }
      if (!this.data.kindPic) {
        my.alert({ content: '请上传一张实物的照片' });
        return false;
      }
      if (!this.data.kindLink) {
        my.alert({ content: '请输入实物的商品链接地址' });
        return false;
      }
      if (!this.data.kindItem) {
        my.alert({ content: '设置实物总个数' });
        return false;
      }
      if (!this.data.kindUser_item) {
        my.alert({ content: '设置实物的每人限量' });
        return false;
      }
      //my.alert({ content: JSON.stringify(this.data) });
      var apiurl = 'App.ShopRedsInfos.SaveInKind';
      var params = {
        "name": this.data.kindName,
        "pic": this.data.kindPic,
        'link': this.data.kindLink,
        'item': this.data.kindItem,
        "user_item": this.data.kindUser_item,
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        self.setData({
          modals3:false
        })
        self.getKindList(1)
        my.alert({ title: '实物礼品创建成功!' });
      });
    },
    tabsActive(e) {
      var id = e.detail.value;
      this.setData({
        showModal1: false,
        showModal2: false,
        showModal3: false,
      });
      switch (id) {
        case "1":
          this.setData({
            showModal1: true,
          })
          this.getShopCouponList(1);
          break;
        case "2":
          this.setData({
            showModal2: true,
          })
          break;
        case "3":
          this.setData({
            showModal3: true,
          })
          this.getKindList(1);
          break;
      }
    },
    /** 获取店铺优惠券列表 */
    getShopCouponList(pageNum) {
      var apiurl = 'App.ShopRedsInfos.GetUserCoupon';
      var params = {
        "page": pageNum,
        "pagesize": this.data.pagesize,
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        // my.alert({ title: JSON.stringify(e) });
        // my.alert({ title: e.page_info.total });
        // console.log(333333333)
        self.setData({
          listData1: e.list,
          shop_total: e.page_info.total
        });
        // my.alert({ title: JSON.stringify(self.data.shop_total) });
      });
    },
    /** 获取实物礼品列表 */
    getKindList(pageNum) {
      var apiurl = 'App.ShopRedsInfos.GetInKind';
      var params = {
        "page": pageNum,
        "pagesize": this.data.pagesize,
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        self.setData({
          listData3: e.list,
          total3: e.page_info.total
        });
      });
    },
    //店铺翻页
    pageShop(current){
      this.getShopCouponList(current.detail.value);
    },
    /** 实物翻页 */
    pageGift(current) {
      this.getKindList(current.detail.value);
    },
    onChange(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      console.log(name + ' onChange:', value);
      this.setData({ [name]: value });
    },
    ontabs1(e) {
      const activeKey = e.detail.value
      console.log('activeKey, >>>>>>>>>>>>>>', this, activeKey)
      this.setData({ activeKey });
    },
    // 上架
    handleS(evt) {
      var x = evt.currentTarget.dataset.x;
      var self = this;
      my.confirm({
        title: '确认',
        content: '真的上架吗?',
        success: (result) => {
          if (result.confirm) {
            var apiurl = 'App.ShopRedsInfos.ModifyInKindStatus';
            var params = {
              "coupon_id": x.record.coupon_id,
              'type': x.record.type,
              "status": 1
            };
            console.log(params);
            var app = getApp();
            app.xdapis(apiurl, params, function (e) {
              my.hideLoading();
              my.showToast({ content: '上架成功!' });
              self.getKindList(1);
            });
          }
        }
      });
      //my.alert({ title: JSON.stringify(record) });
    },
    // 下架
    handleX(evt) {
      console.log(evt);
      var x = evt.currentTarget.dataset.x;
      var self = this;
      my.confirm({
        title: '确认',
        content: '真的下架吗?',
        success: (result) => {
          if (result.confirm) {
            var apiurl = 'App.ShopRedsInfos.ModifyInKindStatus';
            var params = {
              "coupon_id": x.record.coupon_id,
              'type': x.record.type,
              "status": 0
            };
            console.log(params);
            var app = getApp();
            app.xdapis(apiurl, params, function (e) {
              my.hideLoading();
              my.showToast({ content: '下架成功!' });
              console.log(e);
              self.getKindList(1);
            });
          }
        }
      });
    }
  }

});
